package cc.zy.base.hr.controller;

import cc.zy.base.common.annotation.ControllerEndpoint;
import cc.zy.base.common.controller.BaseController;
import cc.zy.base.common.entity.FebsConstant;
import cc.zy.base.common.entity.FebsResponse;
import cc.zy.base.common.entity.QueryRequest;
import cc.zy.base.common.utils.DateUtil;
import cc.zy.base.common.utils.FebsUtil;
import cc.zy.base.hr.entity.ScoreIntegral;
import cc.zy.base.hr.service.IScoreIntegralService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

@Slf4j
@Validated
@Controller
@RequiredArgsConstructor
public class ScoreIntegralController extends BaseController {

    @Resource
    private final IScoreIntegralService scoreIntegralService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreIntegral/detail/{id}")
    public String scoreIntegralDetail(@PathVariable Integer id, Model model) {
        resolveScoreIntegralModel(id, model, true);
        return FebsUtil.hrView("scoreIntegral/scoreIntegralDetail");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreIntegral/update/{id}")
    public String scoreIntegralUpdate(@PathVariable Integer id, Model model) {
        resolveScoreIntegralModel(id, model, false);
        return FebsUtil.hrView("scoreIntegral/scoreIntegralUpdate");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "scoreIntegral/add")
    public String scoreIntegralAdd() {
        return FebsUtil.hrView("scoreIntegral/scoreIntegralAdd");
    }

    private void resolveScoreIntegralModel(Integer id, Model model, Boolean transform) {
        ScoreIntegral scoreIntegral = scoreIntegralService.findByScoreIntegralId(id);
        if (scoreIntegral.getCreateDate() != null) {
            model.addAttribute("createDate", DateUtil.getDateFormat(scoreIntegral.getCreateDate(), DateUtil.FULL_TIME_SPLIT_PATTERN));
        }
        model.addAttribute("scoreIntegral", scoreIntegral);
    }

    @GetMapping("scoreIntegral/list")
    @ResponseBody
    public FebsResponse scoreIntegralList(QueryRequest request, ScoreIntegral scoreIntegral) {

        log.debug("前台传来的对象是：" + scoreIntegral);

        Map<String, Object> dataTable = getDataTable(scoreIntegralService.findScoreIntegrals(request, scoreIntegral));
        return new FebsResponse().success().data(dataTable);
    }

    @PostMapping("scoreIntegral/add")
    @ResponseBody
    @ControllerEndpoint(operation = "新增积分记录", exceptionMessage = "新增积分记录异常")
    public FebsResponse addScoreIntegral(@Valid ScoreIntegral scoreIntegral) {

        log.debug("前台传来的对象是：" + scoreIntegral);

        scoreIntegralService.createScoreIntegral(scoreIntegral);
        return new FebsResponse().success();
    }

    @GetMapping("scoreIntegral/delete/{scoreIntegralIds}")
    @ControllerEndpoint(operation = "删除积分记录", exceptionMessage = "删除积分记录异常")
    @ResponseBody
    public FebsResponse deleteScoreIntegrals(@NotBlank(message = "{required}") @PathVariable String scoreIntegralIds) {

        String[] ids = scoreIntegralIds.split(StringPool.COMMA);
        scoreIntegralService.deleteScoreIntegrals(ids);
        return new FebsResponse().success();
    }

    @PostMapping("scoreIntegral/update")
    @ControllerEndpoint(operation = "修改积分记录", exceptionMessage = "修改积分记录异常")
    @ResponseBody
    public FebsResponse updateScoreIntegral(@Valid ScoreIntegral scoreIntegral) {

        log.debug("修改标签id：" + scoreIntegral.getId());

        scoreIntegralService.updateScoreIntegral(scoreIntegral);
        return new FebsResponse().success();
    }

    @GetMapping("scoreIntegral/excel")
    @ControllerEndpoint(exceptionMessage = "导出Excel异常")
    public void export(HttpServletResponse response, ScoreIntegral scoreIntegral) {

        log.debug(String.format("response = %s", response));
        log.debug(String.format("college = %s", scoreIntegral));

        List<ScoreIntegral> scoreIntegrals = scoreIntegralService.findScoreIntegralListNotPage(scoreIntegral);
        ExcelKit.$Export(ScoreIntegral.class, response).downXlsx(scoreIntegrals, false);
    }

    /**
     * 方法描述：通过ID查找积分记录详情
     *
     * @param scoreIntegralId 积分记录id
     */
    @GetMapping("scoreIntegral/findById/{scoreIntegralId}")
    @ResponseBody
    public FebsResponse findById(@PathVariable Integer scoreIntegralId) {

        log.debug(String.format("scoreIntegralId = %s", scoreIntegralId));

        ScoreIntegral scoreIntegral = scoreIntegralService.findByScoreIntegralId(scoreIntegralId);
        return new FebsResponse().success().data(scoreIntegral);
    }

    /**
     * 增加功能中，查询前台输入的全称或简称是否存userAdd.html在
     *
     * @param1 staffId
     * @param2 scoreDesc
     * @return Boolean false为重复++
     *
     * true为不重复
     */
    @GetMapping("scoreIntegral/isRepetitive")
    @ResponseBody
    public Boolean isRepetitive(Integer staffId, String scoreDesc) {

        return scoreIntegralService.isRepetitive(staffId, scoreDesc);
    }

}
